package com.example.skytakeoutserver.config.cpn.task

import com.example.serverbase.ext.Log
import com.example.skytakeoutserver.handler.WebSocketHandler
import lombok.RequiredArgsConstructor
import org.springframework.scheduling.annotation.Scheduled
import org.springframework.stereotype.Component
import java.time.Instant
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter


/**
 * Websocket 定时任务
 */
@Component
@RequiredArgsConstructor
class WebSocketTask(
    val webSocketHandler: WebSocketHandler,
) {
    /**
     * 通过websocket 每隔5s 向客户端发送消息
     */
    @Scheduled(cron = "0/5 * * * * ?")
    fun sendMessageToClient() {
        Log.info("Sending message to client: {}", Instant.now())

        webSocketHandler.sendToAllClient(
            "这是来自服务端的消息: " +
                    DateTimeFormatter.ofPattern("HH:mm:ss")
                        .format(LocalDateTime.now())
        )
    }

}